/**
 * Created on 2007-05-16 14:13
 */
package game.frames;

import game.Settings;
import game.score.HighscoreList;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 * @author David Wallin
 * 
 * The highscore board
 */
public class HighscoreFrame extends BasicFrame
{
	/**
	 * Serializable interface forces this to be declared
	 */
	private static final long		serialVersionUID = -5467769835158784271L;
	
	public static int				MAX_NAME_LENGTH = 12;
	
	private JLabel					m_Header;
	private JLabel					m_Result;
	private JTextArea				m_ScoreBoard;
	private JButton					m_Main;
	
	private JButton					m_Easy;
	private JButton					m_Medium;
	private JButton					m_Hard;
	
	/**
	 * @param title
	 */
	public HighscoreFrame()
	{
		super(new GridBagLayout());
	}
	
	/* (non-Javadoc)
	 * @see frames.BasicFrame#initializeComponent()
	 */
	@Override
	protected void initializeComponent()
	{
		this.setBackground(Color.black);
		GridBagLayout gridBag = (GridBagLayout)this.getLayout();
		
		GridBagConstraints resultConstraints = new GridBagConstraints();
		resultConstraints.insets = new Insets(10, 0, 0, 0);
		resultConstraints.gridx = 1;
		resultConstraints.gridy = 0;
		resultConstraints.gridwidth = 1;
		resultConstraints.weightx = 0.5;
		
		GridBagConstraints headerConstraints = new GridBagConstraints();
		headerConstraints.insets = new Insets(10, 0, 0, 0);
		headerConstraints.gridx = 1;
		headerConstraints.gridy = 1;
		headerConstraints.gridwidth = 1;
		headerConstraints.weightx = 0.5;
		
		GridBagConstraints difficultyPanelConstraints = new GridBagConstraints();
		difficultyPanelConstraints.gridx = 0;
		difficultyPanelConstraints.gridy = 2;
		difficultyPanelConstraints.gridwidth = 3;
		
		GridBagConstraints scoreBoardConstraints = new GridBagConstraints();
		scoreBoardConstraints.insets = new Insets(10, 0, 0, 0);
		scoreBoardConstraints.gridx = 0;
		scoreBoardConstraints.gridy = 3;
		scoreBoardConstraints.gridwidth = 3;
		
		GridBagConstraints mainConstraints = new GridBagConstraints();
		mainConstraints.gridx = 1;
		mainConstraints.gridwidth = 1;
		mainConstraints.weightx = 0.5;
		mainConstraints.weighty = 1.5;
		mainConstraints.anchor = GridBagConstraints.SOUTH;
		
		m_Result = new JLabel("Your score was " + (m_InputManager.getHighscoreAdded() ? "successfully" : "not") + " added!");
		m_Header = new JLabel("Top 10 players, difficulty: ");
		m_ScoreBoard = new JTextArea("All the highscores displayed here\n\n\rone by one\non rows like this..\n\nScorekeepers of the world unite!\n\n\nLet's fill'er up :D\nMoar?");
		m_Main = new JButton("Main screen");
		
		m_Easy = new JButton("Easy");
		m_Medium = new JButton("Medium");
		m_Hard = new JButton("Hard");
		
		JPanel difficultyPanel = new JPanel(new GridLayout(1, 3));
		difficultyPanel.setBackground(Color.black);
		difficultyPanel.add(m_Easy);
		difficultyPanel.add(m_Medium);
		difficultyPanel.add(m_Hard);
		
		Font f = new Font("Times", Font.BOLD, 14);
		m_Result.setFont(f);
		m_Result.setHorizontalAlignment(JLabel.CENTER);
		m_Result.setVerticalAlignment(JLabel.CENTER);
		m_Result.setBackground(Color.black);
		m_Result.setForeground(Color.white);
		
		m_Header.setFont(f);
		m_Header.setHorizontalAlignment(JLabel.CENTER);
		m_Header.setVerticalAlignment(JLabel.CENTER);
		m_Header.setBackground(Color.black);
		m_Header.setForeground(Color.gray);
		
		m_ScoreBoard.setEditable(false);
		m_ScoreBoard.setBackground(Color.black);
		m_ScoreBoard.setForeground(Color.white);
		
		m_Main.addActionListener(this);
		m_Easy.addActionListener(this);
		m_Medium.addActionListener(this);
		m_Hard.addActionListener(this);
		
		this.addComponent(m_Result, gridBag, resultConstraints);
		this.addComponent(m_Header, gridBag, headerConstraints);
		this.addComponent(difficultyPanel, gridBag, difficultyPanelConstraints);
		this.addComponent(m_ScoreBoard, gridBag, scoreBoardConstraints);
		this.addComponent(m_Main, gridBag, mainConstraints);
		
		this.doLayout();
	}
	
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent event)
	{
		Object source = event.getSource();
		
		if(source == m_Main)
		{
			this.updateFrameSwitchListeners(Frames.MAIN_MENU);
		}
		
		if(source == m_Easy)
		{
			m_Settings.setDifficulty(Settings.DifficultyLevels.EASY);
			this.updateFrameSwitchListeners(Frames.HIGHSCORES);
		}
		
		if(source == m_Medium)
		{
			m_Settings.setDifficulty(Settings.DifficultyLevels.MEDIUM);
			this.updateFrameSwitchListeners(Frames.HIGHSCORES);
		}
		
		if(source == m_Hard)
		{
			m_Settings.setDifficulty(Settings.DifficultyLevels.HARD);
			this.updateFrameSwitchListeners(Frames.HIGHSCORES);
		}
	}
	
	public void setResultAndPopulateScoreBoard()
	{
		m_Result.setText("");
		m_Header.setText("Top 10 players, difficulty: " + m_Settings.getDifficulty().toString().toLowerCase());
		
		if(m_InputManager.getHasHighscoreAddedResult())
		{
			m_Result.setText("Your score was " + (m_InputManager.getHighscoreAdded() ? "successfully" : "not") + " added!");
		}
		
		HighscoreList highscoreList = new HighscoreList();
		highscoreList.readHighscores();
		//m_ScoreBoard.setText("All the highscores displayed here\n\n\rone by one\non rows like this..\n\nScorekeepers of the world unite!\n\n\nLet's fill'er up :D\nMoar?");
		m_ScoreBoard.setText("NAME\tLEVEL\tSCORE\n\n" + highscoreList.toString());
	}
}
